package com.mef.smsplat.service;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hc24.util.StringUtil;
import com.mef.smsplat.dao.AdminInfoDao;
import com.mef.smsplat.dao.RoleInfoDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.EntityDao;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.AdminInfoDomain;
import com.mef.smsplat.domain.RoleInfoDomain;
import com.mef.smsplat.service.base.BaseService;
import com.mef.smsplat.util.Constants;

@Service
@Transactional
public class AdminInfoService extends BaseService<AdminInfoDomain, String> {
	
	private AdminInfoDao adminInfoDao;
	private RoleInfoDao roleInfoDao;
	
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setAdminInfoDao(AdminInfoDao adminInfoDao) {
		this.adminInfoDao = adminInfoDao;
	}

	public void setRoleInfoDao(RoleInfoDao roleInfoDao) {
		this.roleInfoDao = roleInfoDao;
	}

	@Override
	public EntityDao getEntityDao() {
		return this.adminInfoDao;
	}
	
	/**
	 * 登录
	 * @param username
	 * @param password
	 * @return
	 */
	public AdminInfoDomain login(String username,String password){
		AdminInfoDomain admin = adminInfoDao.query(username, password);
		//这里记录最后登陆时间业务逻辑处理
		if(admin != null){
			admin.setLoginTime(new Date());
			save(admin);
		}
		return admin;
	}
	
	/**
	 * 查询账号是否唯一
	 * @param name
	 * @param id
	 * @return
	 */
	@Transactional(readOnly=true)
	public boolean isUnique(String name, String id){
		boolean isUnique = adminInfoDao.queryByNameAndId(name, id);
		return isUnique;
	}
	
	/**
	 * 分页查询
	 * @param username
	 * @param dgm
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page<AdminInfoDomain> query(String adminName,DataGridModel dgm){
		Page<AdminInfoDomain> page = adminInfoDao.query(adminName, dgm);
		List<AdminInfoDomain> list = page.getRows();
		for(AdminInfoDomain admin:list){
			RoleInfoDomain role = roleInfoDao.queryByRoleId(admin.getRoleId());
			if(role!=null){
				admin.setRoleId(role.getRoleName());
			}
		}
		return page;
	}
	
	/**
	 * 重置密码
	 */
	public void resetPassword(String[] ids){
		//循环查询对象，并且执行重置密码操作
		for (int i = 0; i < ids.length; i++) {
			AdminInfoDomain admin = adminInfoDao.getById(ids[i]);
			if(admin!=null){
				admin.setAdminPassword(StringUtil.md5(Constants.PASSWORD));
				adminInfoDao.update(admin);
			}
		}
	}
	
	/**
	 * 启用
	 */
	public void enable(String[] ids){
		//循环查询对象，并且执行操作
		for (int i = 0; i < ids.length; i++) {
			AdminInfoDomain admin = adminInfoDao.getById(ids[i]);
			if(admin!=null){
				admin.setStatus(Constants.NORMAL);
				adminInfoDao.update(admin);
			}
		}
	}
	/**
	 * 停用
	 */
	public void disable(String[] ids){
		//循环查询对象，并且执行操作
		for (int i = 0; i < ids.length; i++) {
			AdminInfoDomain admin = adminInfoDao.getById(ids[i]);
			if(admin!=null){
				admin.setStatus(Constants.STOP);
				adminInfoDao.update(admin);
			}
		}
	}
}
